﻿using System;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using ShopB2C.Enums;

namespace ShopB2C.Helper {
    /// <summary>
    /// 配置类操作
    /// </summary>
    public static class ConfigHelper {
        private static ConfigOperate _co;
        private static string[] _keys;

        static ConfigHelper() {
            _co = new ConfigOperate();
            _keys = new string[] { "system", "imagerepeat", "search", "newsconfig" };
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        public static void Save() {
            _co.Save();
        }
        /// <summary>
        /// 获取系统配置
        /// </summary>
        /// <param name="cnn">配置节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <returns>节点属性 value 的值</returns>
        public static string GetValue(ConfigNodeNames cnn, string key) {
            return _co.GetValue<string>(_keys[(int)cnn], key);
        }
        /// <summary>
        /// 获取系统配置
        /// </summary>
        /// <param name="cnn">配置节点名称</param>
        /// <param name="parentNodeName">父节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <returns>节点属性 value 的值</returns>
        public static string GetValue(ConfigNodeNames cnn, string parentNodeName, string key) {
            return _co.GetValue<string>(_keys[(int)cnn], parentNodeName, key);
        }
        /// <summary>
        /// 获取系统配置
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="cnn">配置节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <returns>节点属性 value 的值</returns>
        public static T GetValue<T>(ConfigNodeNames cnn, string key) {
            return _co.GetValue<T>(_keys[(int)cnn], string.Empty, key);
        }
        /// <summary>
        /// 获取系统配置
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="cnn">配置节点名称</param>
        /// <param name="parentNodeName">父节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <returns>节点属性 value 的值</returns>
        public static T ConfigSystem<T>(ConfigNodeNames cnn, string parentNodeName, string key) {
            return _co.GetValue<T>(_keys[(int)cnn], parentNodeName, key);
        }
        /// <summary>
        /// 获取图片轮换配置节点
        /// </summary>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <param name="value">节点值</param>
        public static void SetValue(ConfigNodeNames cnn, string key, string value) {
            _co.SetValue(_keys[(int)cnn], string.Empty, key, value);
        }
        /// <summary>
        /// 获取图片轮换配置节点
        /// </summary>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="parentNodeName">父节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <param name="value">节点值</param>
        public static void SetValue(ConfigNodeNames cnn, string parentNodeName, string key, string value) {
            _co.SetValue(_keys[(int)cnn], parentNodeName, key, value);
        }
        /// <summary>
        /// 获取图片轮换配置节点
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <param name="value">节点值</param>
        public static void SetValue<T>(ConfigNodeNames cnn, string key, T value) {
            _co.SetValue<T>(_keys[(int)cnn], string.Empty, key, value);
        }
        /// <summary>
        /// 获取图片轮换配置节点
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="parentNodeName">父节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <param name="value">节点值</param>
        public static void SetValue<T>(ConfigNodeNames cnn, string parentNodeName, string key, T value) {
            _co.SetValue<T>(_keys[(int)cnn], parentNodeName, key, value);
        }
    }

    #region 获取或写入配置类

    /// <summary>
    /// 获取或写入配置类
    /// </summary>
    internal class ConfigOperate {
        private const string _filePath = "~/Config/SiteConfig.config";
        private string _path;
        private XDocument _xd;

        public ConfigOperate() {
            this._path = HttpContext.Current.Server.MapPath(_filePath);
            this._xd = XDocument.Load(this._path);
        }
        /// <summary>
        /// 存入,最后调用此方法保存配置文件
        /// </summary>
        public void Save() {
            this._xd.Save(this._path);
        }

        /// <summary>
        /// 获取系统配置节点
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <returns>节点属性 value 的值</returns>
        public T GetValue<T>(string configNodeName, string key) {
            return this.Get<T>(configNodeName, string.Empty, key);
        }
        /// <summary>
        /// 获取系统配置节点
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="parentNodeName">父节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <returns>节点属性 value 的值</returns>
        public T GetValue<T>(string configNodeName, string parentNodeName, string key) {
            return this.Get<T>(configNodeName, parentNodeName, key);
        }
        /// <summary>
        /// 获取图片轮换配置节点
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <param name="value">节点值</param>
        public void SetValue<T>(string configNodeName, string key, T value) {
            this.Set<T>(configNodeName, string.Empty, key, value);
        }
        /// <summary>
        /// 获取图片轮换配置节点
        /// </summary>
        /// <typeparam name="T">节点值类型</typeparam>
        /// <param name="configNodeName">配置节点名称</param>
        /// <param name="parentNodeName">父节点名称</param>
        /// <param name="key">节点属性 key 的值</param>
        /// <param name="value">节点值</param>
        public void SetValue<T>(string configNodeName, string parentNodeName, string key, T value) {
            this.Set<T>(configNodeName, parentNodeName, key, value);
        }

        // 取值
        private T Get<T>(string p1, string p2, string p3) {
            XElement xes = string.IsNullOrEmpty(p2)
                ? this._xd.Root.Element(XName.Get(p1))
                : this._xd.Root.Element(XName.Get(p1)).Element(XName.Get(p2));
            XElement xe = xes.Descendants().First(x => {
                return x.Attribute(XName.Get("key")).Value == p3;
            });
            XAttribute xa = xe.Attribute(XName.Get("value"));
            string s = xa != null ? xa.Value : string.Empty;

            return (T)Convert.ChangeType(s, typeof(T));
        }
        // 赋值
        private void Set<T>(string p1, string p2, string p3, T value) {
            XElement xes = string.IsNullOrEmpty(p2)
                ? this._xd.Root.Element(XName.Get(p1))
                : this._xd.Root.Element(XName.Get(p1)).Element(XName.Get(p2));

            XElement xe = xes.Descendants().First(x => {
                return x.Attribute(XName.Get("key")).Value == p3;
            });

            XAttribute xa = xe.Attribute(XName.Get("value"));
            xa.SetValue(value);
        }

    }

    #endregion
}
